package org.jmc.refmanager.web.wicket.model;

import org.apache.wicket.injection.web.InjectorHolder;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.jmc.refmanager.domain.model.User;
import org.jmc.refmanager.domain.service.UserManager;

public class UserModel implements IModel<User> {
	private static final long serialVersionUID = 1L;

	private String matricule;
	private transient User user;
	
	@SpringBean(name="userManager")
	private UserManager userManager;
	
	/**
	 * loadUserByUsername
	 */
	public UserModel(User user) {
		super();
		InjectorHolder.getInjector().inject(this);
		this.matricule = user.getMatricule();
		this.user = user;
	}

	@Override
	public void detach() {
		this.user = null;
	}

	@Override
	public User getObject() {
		if (this.user == null && this.matricule != null ) {
			this.user = userManager.getByMatricule(this.matricule);
		}
		
		return this.user;
	}

	@Override
	public void setObject(User user) {
		throw new UnsupportedOperationException("Model " 
				+ getClass() + " does not support setObject(Object)");
	}

}
